package com.iespring.service.app.topic;

import com.iespring.bean.admin.promotion.po.MallTopic;
import com.iespring.bean.admin.user.entity.MallGoods;
import com.iespring.bean.app.topic.vo.TopicData;
import com.iespring.bean.app.topic.vo.WxGoodsVo;
import com.iespring.bean.app.topic.vo.WxTopicForListVo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.MallGoodsMapper;
import com.iespring.mapper.admin.MallSystemMapper;
import com.iespring.mapper.admin.MallTopicMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author:ZhangShuPeng
 * @Date:2023/01/02/13:36
 * @Description:
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    MallTopicMapper mallTopicMapper;

    @Autowired
    MallGoodsMapper mallGoodsMapper;

    @Autowired
    MallSystemMapper mallSystemMapper;

    //已测试
    @Override
    public BaseRespVo list(Integer page, Integer limit) {

//        PageHelper.startPage(page, size);

        List<WxTopicForListVo> wxTopicForListVos = new ArrayList<>();
        List<MallTopic> mallTopics = mallTopicMapper.selectAllTopic();
        for (MallTopic mallTopic : mallTopics) {
            WxTopicForListVo wxTopicForListVo = new WxTopicForListVo();
            BeanUtils.copyProperties(mallTopic, wxTopicForListVo);
//            wxTopicForListVo.setId(mallTopic.getId());
//            wxTopicForListVo.setPicUrl(mallTopic.getPicUrl());
//            wxTopicForListVo.setPrice(mallTopic.getPrice());
//            wxTopicForListVo.setReadCount(mallTopic.getReadCount());
//            wxTopicForListVo.setSubtitle(mallTopic.getSubtitle());
//            wxTopicForListVo.setTitle(mallTopic.getTitle());

            wxTopicForListVos.add(wxTopicForListVo);
        }

        CommonData<WxTopicForListVo> commonData = new CommonData<>();

        commonData.setLimit(limit);
        commonData.setPage(page);
        commonData.setTotal(mallTopics.size());
        int pages;
        if ((mallTopics.size() % limit) == 0) {
            pages = mallTopics.size() / limit;
        } else {
            pages = (mallTopics.size() / limit) + 1;
        }

        commonData.setPages(pages);

        List<WxTopicForListVo> wxTopicForListVos1 = new ArrayList<>();
        for (int i = limit * (page - 1); i < page * limit; i++) {
            // limit = 10 i = 10 break
            if (wxTopicForListVos.size() < i) {
                break;
            }
            wxTopicForListVos1.add(wxTopicForListVos.get(i));
        }

        commonData.setList(wxTopicForListVos1);

        BaseRespVo baseRespVo = BaseRespVo.ok(commonData);

        return baseRespVo;
    }

    @Override
    public BaseRespVo detail(int id) {

        MallTopic mallTopic = mallTopicMapper.selectByPrimaryKey(id);
        String[] goods = mallTopic.getGoods();
        WxGoodsVo[] wxGoodsVos = new WxGoodsVo[goods.length];
        for (String good : goods) {
            int goodsId = Integer.parseInt(good);
            MallGoods mallGoods = mallGoodsMapper.selectByPrimaryKey(goodsId);
            WxGoodsVo wxGoodsVo = new WxGoodsVo();
            BeanUtils.copyProperties(mallGoods, wxGoodsVo);
//            wxGoodsVo.setBrief(mallGoods.getBrief());
//            wxGoodsVo.setCounterPrice(mallGoods.getCounterPrice());
//            wxGoodsVo.setId(mallGoods.getId());
//            wxGoodsVo.setIsHot(mallGoods.getIsHot());
//            wxGoodsVo.setIsNew(mallGoods.getIsNew());
//            wxGoodsVo.setName(mallGoods.getName());
//            wxGoodsVo.setPicUrl(mallGoods.getPicUrl());
//            wxGoodsVo.setRetailPrice(mallGoods.getRetailPrice());
            for (int i = 0; i < goods.length; i++) {
                wxGoodsVos[i] = wxGoodsVo;
            }
        }

        TopicData topicData = new TopicData(wxGoodsVos, mallTopic);
        BaseRespVo baseRespVo = BaseRespVo.ok(topicData);


        return baseRespVo;
    }

    @Override
    public BaseRespVo related(int id) {

        String market_wx_catlog = mallSystemMapper.selectSystemValueByKeyName("market_wx_catlog_goods");
        int limit = Integer.parseInt(market_wx_catlog);
        int page = 1;
        List<MallTopic> mallTopics = mallTopicMapper.selectAllTopic();
        int total = mallTopics.size();
        int pages;
        if (total % limit == 0) {
            pages = total / limit;
        } else {
            pages = (total / limit) + 1;
        }
        ArrayList<MallTopic> list = new ArrayList<>();
        //TODU
        for (MallTopic mallTopic : mallTopics) {
//            for (int i = 0; i < limit; i++) {
                list.add(mallTopic);
//            }
        }
        CommonData<MallTopic> data = new CommonData<>(total, pages, limit, page, list);

        BaseRespVo baseRespVo = BaseRespVo.ok(data);

        return baseRespVo;
    }
}
